package com.tyf.common.web.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.tyf.common.domain.search.Searchable;
import com.tyf.common.vo.DetailSearchVO;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

public class EudFastJsonHttpMessageConverter extends
		FastJsonHttpMessageConverter {
	
	/**
	 * <p>增加针对Searchable处理，因为searchable无法自动json转换</p>
	 * <p>采用人工干预处理的方式,创建Searchable对象</p>
	 */
	@Override
	public Object read(Type type, //
			Class<?> contextClass, //
			HttpInputMessage inputMessage ) throws IOException, HttpMessageNotReadableException {
		
		if(type.getTypeName().equals(Searchable.class.getName())){
			InputStream in = inputMessage.getBody();
			byte[] bytes = IOUtils.toByteArray(in);
			Object object = JSON.parse(bytes, this.getFastJsonConfig().getFeatures());
			
			JSONObject jsonObj = (JSONObject)object;
			JSONObject jsonPage = jsonObj.getJSONObject("page");
			JSONArray jsonFilter = jsonObj.getJSONArray("searchFilters");
			JSONArray jsonSort = jsonObj.getJSONArray("sort");
			JSONObject jsonDetailSearch = jsonObj.getJSONObject("detailSearch");
			String strDetailSearchQl = jsonObj.getString("detailSearchQl");
			
			Searchable search = Searchable.newSearchable();
			if(jsonFilter != null && jsonFilter.size() > 0){
				//查询参数
				for(int i = 0; i < jsonFilter.size(); i++){
					JSONObject jsonSearchFileter = jsonFilter.getJSONObject(i);
					String key = jsonSearchFileter.getString("key");
					if (StringUtils.endsWithIgnoreCase(key, "in")) {
						search.addSearchParam(key, jsonSearchFileter.getJSONArray("value"));
					}else{
						search.addSearchParam(key, jsonSearchFileter.getString("value"));
					}
				}
			}
			//排序
			if(jsonSort != null && jsonSort.size() > 0){
				for(int i = 0; i < jsonSort.size(); i++){
					JSONObject jsonSortObj = jsonSort.getJSONObject(i);
					String property = jsonSortObj.getString("property");
					String value = jsonSortObj.getString("direction");
					if(value.equals(Sort.Direction.ASC.toString())){
						search.addSort(Sort.Direction.ASC, property);
					}else if(value.equals(Sort.Direction.DESC)){
						search.addSort(Sort.Direction.DESC, property);
					}
				}
			}
			
			//分页参数
			if(jsonPage != null && jsonPage.size()>0){
				int pageNumber = jsonPage.getInteger("pageNumber");
				int pageSize = jsonPage.getInteger("pageSize");
				search.setPage(pageNumber,pageSize);
			}
			//detailSearch
			if(jsonDetailSearch != null){
				DetailSearchVO searchVO = JSON.parseObject(jsonDetailSearch.toJSONString(), DetailSearchVO.class);
				search.setDetailSearch(searchVO);
			}
			//DetailSearchQl
			if(StringUtils.isNotBlank(strDetailSearchQl)){
				search.setDetailSearchQl(new StringBuilder(strDetailSearchQl));
			}
			
			return search;
		}
		return super.read(type, contextClass, inputMessage);
	}
}
